package MST;

public class MST0106 {
    public String compressString(String S) {
        if (S.length()==0)return S;
        StringBuilder res = new StringBuilder();
        char d = S.charAt(0);
        int sum = 1;
        for (int i = 1; i < S.length(); i++) {
            char c = S.charAt(i);
            if (c == d) {
                sum++;
            } else {
                res.append(d);
                res.append(sum);
                d = c;
                sum = 1;
            }
        }
        res.append(d);
        res.append(sum);
        return res.length() < S.length() ? res.toString() : S;
    }
}
